//
//  YXTeamViewController.m
//  跑跑快销
//
//  Created by 杨潇 on 14-4-28.
//  Copyright (c) 2014年 ylife. All rights reserved.
//

#import "YXTeamViewController.h"
#import "TNTeamCollectionViewCell.h"
#import "TNMenberDetailViewController.h"
#import "TNNotificationViewController.h"
#import "TNTeamFormViewController.h"
#import "TNHeadmanViewController.h"


@interface YXTeamViewController ()

@end

@implementation YXTeamViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
       
    }
    return self;
}

- (void)viewDidLoad
{
    
    
    //查询我是否创建团队
    NSDictionary *userInfoDic = [[YXWindowInfo sharedWindowInfo]userInfoDic];
    [NSURLConnection sendRequestWithHeadStr:[userInfoDic objectForKey:@"ServerAddress"]
                                  headValue:nil
                               serviceToken:[userInfoDic objectForKey:@"serviceToken"]
                                       text:[NSDictionary dictionaryWithObjectsAndKeys:@"getMyteam",@"type",[[userInfoDic objectForKey:@"userInfo"] objectForKey:@"AccountID"],@"memberId",[[userInfoDic objectForKey:@"userInfo"] objectForKey:@"AccountID"],@"userId",[userInfoDic objectForKey:@"ConnectionString"],@"ConnectionString", nil]
                                  dataBlock:^(NSDictionary *dic) {
                                      if ([[dic objectForKey:@"InformationList"] length] != 0) {
                                          //根据团队号获取团队人员信息
                                          [NSURLConnection sendRequestWithHeadStr:[userInfoDic objectForKey:@"ServerAddress"]
                                                                        headValue:[userInfoDic objectForKey:@"token"]
                                                                     serviceToken:[userInfoDic objectForKey:@"serviceToken"]
                                                                             text:[NSDictionary dictionaryWithObjectsAndKeys:@"getTeamMember",@"type",[[userInfoDic objectForKey:@"userInfo"] objectForKey:@"AccountID"],@"userId",[dic objectForKey:@"InformationList"],@"teamID",[userInfoDic objectForKey:@"ConnectionString"],@"ConnectionString", nil] dataBlock:^(NSDictionary *dic) {
                                                                                 _dataArray = [dic objectForKey:@"membersList"];
                                                                                 NSLog(@"-------%@",_dataArray);
                                                                                 
                                                                                 
                                                                                 _teamArray = [NSMutableArray array];
                                                                                 for (NSDictionary *dic in _dataArray) {
                                                                                     if ([[dic objectForKey:@"AccountID"] isEqualToString:[[userInfoDic objectForKey:@"userInfo"] objectForKey:@"AccountID"]]) {
                                                                                         [_teamView.myButton setTitle:[dic objectForKey:@"AccountName"] forState:UIControlStateNormal];
                                                                                         
                                                                                         NSURL *url = [NSURL URLWithString:[@"http://192.168.1.222:87/userIcon/" stringByAppendingString:[dic objectForKey:@"AccountIconUrl"]]];
                                                                                         NSData *data = [NSData dataWithContentsOfURL:url];
                                                                                         UIImage *image = [[UIImage alloc]initWithData:data];
                                                                                         [_teamView.myButton setImage:image forState:UIControlStateNormal];
                                                                                     }
                                                                                     else
                                                                                     {
                                                                                         [_teamArray addObject:dic];
                                                                                     }
                                                                                     [_teamView.collectionView reloadData];
                                                                                     
                                                                                 }
                                                                                 
                                                                             }];
                                      }
                                      else{
                                          NSLog(@"没有创建团队");
                                          UIImageView *imagview = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"a.png"]];
                                          self.view = imagview;
                                      }
                                      
                                  }];
    
    
    [super viewDidLoad];
    [self createSegmentControl];
    self.navigationController.navigationBar.translucent = NO;
    
    //初始化团员界面
    _teamView = [[YXTeamView alloc]init];
    [_teamView createItemsView];
    [_teamView createRelationshipView];
//    _menberDetailView = [[TNMemberDetailView alloc]init];
    [_teamView.notificationButton addTarget:self action:@selector(gotoNotificationViewController) forControlEvents:UIControlEventTouchUpInside];
    [_teamView.teamStatementButton addTarget:self action:@selector(gotoTeamStatementViewController) forControlEvents:UIControlEventTouchUpInside];
    [_teamView.chatButton addTarget:self action:@selector(gotoChatViewController) forControlEvents:UIControlEventTouchUpInside];
    
    _teamView.collectionView.delegate = self;
    _teamView.collectionView.dataSource = self;
    [_teamView.myButton addTarget:self action:@selector(gotoHeadmanViewController) forControlEvents:UIControlEventTouchUpInside];
    self.view = _teamView;

    
    
    _lines = 17 / 3 + 1;
    _lastLine = 17 % 3;
    [_teamView.collectionView setContentSize:CGSizeMake(320, _lines * 100 + 100)];
    
    //更改返回按钮标题
    UIBarButtonItem *backBarButtonItem = [[UIBarButtonItem alloc] init];
    backBarButtonItem.title = @"返回";
    self.navigationItem.backBarButtonItem = backBarButtonItem;
    
}


//创建segmentController
- (void)createSegmentControl
{
    int segmentControlY;
    
    if (ISDEVICE7) {
        segmentControlY = 17;
    }else{
        segmentControlY = 7;
    }
    
    UISegmentedControl *segmentControl = [[UISegmentedControl alloc]initWithFrame:CGRectMake(60.0f, segmentControlY, 200.0f, 30.0f)];
    [segmentControl insertSegmentWithTitle:@"我的团队" atIndex:0 animated:NO];
    [segmentControl insertSegmentWithTitle:@"我的团长" atIndex:1 animated:NO];
    segmentControl.selectedSegmentIndex = 0;
    //更改颜色
    //    segmentControl.tintColor = [UIColor whiteColor];
    self.navigationItem.titleView = segmentControl;
    [segmentControl addTarget:self action:@selector(selectSegmentedcontrolIndex:) forControlEvents:UIControlEventValueChanged];
}

#pragma mark - 按钮事件
- (void)gotoNotificationViewController
{
//    NSLog(@"点击通知按钮");
    [self setHidesBottomBarWhenPushed:YES];
    TNNotificationViewController *viewController = [[TNNotificationViewController alloc]init];
    [self.navigationController pushViewController:viewController animated:YES];
    [self setHidesBottomBarWhenPushed:NO];
}

- (void)gotoTeamStatementViewController
{
//    NSLog(@"点击团队报表按钮");
    TNTeamFormViewController *viewController = [[TNTeamFormViewController alloc]init];
    [self setHidesBottomBarWhenPushed:YES];
    [self.navigationController pushViewController:viewController animated:YES];
    [self setHidesBottomBarWhenPushed:NO];

}

- (void)gotoChatViewController
{
    NSLog(@"点击聊天室按钮");

}

- (void)gotoMenberDetailViewController
{
    [self setHidesBottomBarWhenPushed:YES];
    TNMenberDetailViewController *viewController = [[TNMenberDetailViewController alloc]init];
    [self.navigationController pushViewController:viewController animated:YES];
    [self setHidesBottomBarWhenPushed:NO];
}

- (void)gotoHeadmanViewController
{
    [self setHidesBottomBarWhenPushed:YES];
    TNHeadmanViewController *viewcontroller = [[TNHeadmanViewController alloc]init];
    [self.navigationController pushViewController:viewcontroller animated:YES];
    [self setHidesBottomBarWhenPushed:NO];
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Segmentedcontrol选择事件
-(void)selectSegmentedcontrolIndex:(UISegmentedControl *)seg
{
    NSInteger index = seg.selectedSegmentIndex;
    switch (index) {
        case 0:
            NSLog(@"选中了我的团队");
            
            break;
        case 1:
            NSLog(@"选中了我的团长");
            
            break;
        default:
            break;
    }
}


#pragma mark - UICollectionViewDelegate代理方法

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _teamArray.count;

}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"cell";
    TNTeamCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
    if (!cell) {
        cell = [[TNTeamCollectionViewCell alloc]init];
    }
//    cell.contentView.backgroundColor = [UIColor blueColor];
//    cell.name = array[indexPath.section];
//    cell.imageView.image = [UIImage imageNamed:@"a.png"];
    cell.button.backgroundColor = [UIColor redColor];
//    cell.button.imageView.backgroundColor = [UIColor whiteColor];
//    cell.button.imageView.image = [UIImage imageNamed:@"a.png"];
    
    [cell.button setTitle:[_teamArray[indexPath.row] objectForKey:@"AccountName"] forState:UIControlStateNormal];
    if ([[_teamArray[indexPath.row] objectForKey:@"AccountIconUrl"] length] == 0) {
        [cell.button setImage:[UIImage imageNamed:@"a.png"] forState:UIControlStateNormal];
    }
    else{
        NSURL *url = [NSURL URLWithString:[@"http://192.168.1.222:87/userIcon/" stringByAppendingString:[_teamArray[indexPath.row] objectForKey:@"AccountIconUrl"]]];
        NSData *data = [NSData dataWithContentsOfURL:url];
        UIImage *image = [[UIImage alloc]initWithData:data];
        [cell.button setImage:image forState:UIControlStateNormal];
    }
    [cell.button addTarget:self action:@selector(gotoMenberDetailViewController) forControlEvents:UIControlEventTouchUpInside];
//    cell.button.titleLabel.text = array[indexPath.section];
    return cell;
    
}

@end
